package com.ems.controller;

import com.ems.entity.TMenu;
import com.ems.entity.TUser;
import com.ems.service.TMenuService;
import com.ems.service.TUserService;
import com.ems.util.Response;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/t-menu")
public class TMenuController {
    @Autowired
    private TMenuService tMenuService;

    //新增数据
    @PostMapping("/add")
    public Response add(TMenu tMenu){
        if(tMenuService.findOne(tMenu.getId())==null){
            tMenuService.add(tMenu);
            return new Response(200,"添加成功","",null);
        }else {
            return new Response(400,"用户已存在","",null);
        }
    }

    //删除数据
    @GetMapping("/delete/{id}")
    public Response delete(@PathVariable Integer id){
        if(tMenuService.findOne(id)!=null){
            tMenuService.delete(id);
            return new Response(200,"删除成功","",null);
        }else {
            return new Response(400,"用户不存在","",null);
        }
    }

    //查找数据(单)
    @GetMapping("/findOne/{id}")
    public ResultUtil get(@PathVariable Integer id){
        if (tMenuService.findOne(id)==null){
            return ResultUtil.fail("用户不存在");
        }else {
            return ResultUtil.success(tMenuService.findOne(id));
        }
    }

    //修改数据
    @PostMapping("/update")
    public ResultUtil updateUser(TMenu tMenu) {
        if (tMenuService.update(tMenu)) {
            return ResultUtil.success();
        }else {
            return ResultUtil.fail("修改失败");
        }

    }


    //查找数据(打印所有)
    @GetMapping("/findAll")
    public ResultUtil findAll() {
        if (tMenuService.findAll()==null){
            return ResultUtil.fail("查询失败");
        }else {
            return ResultUtil.success(tMenuService.findAll());
        }
    }
}
